package org.NooLab.docserver.properties;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;


import org.NooLab.docserver.app.up.DocoServStartup;
import org.NooLab.docserver.app.up.IniProperties;
import org.NooLab.docserver.main.DocumentObserverFactory;
import org.NooLab.docserver.util.XmlStringHandling;
import org.NooLab.utilities.datatypes.ValuePair;
import org.NooLab.utilities.files.DFutils;
import org.NooLab.utilities.logging.PrintLog;
import org.NooLab.utilities.strings.ArrUtilities;
import org.NooLab.utilities.strings.StringsUtil;

import com.jamesmurty.utils.XMLBuilder;




/**
 * 
 * 
 *  
	 docoProperties.importTransformationParameterDefaults("standards.ini");

 * 
 */
public class DocoPropertiesHandler implements DocoPropertiesHandlerIntf{

	DocumentObserverProperties docoProperties;
	
	PersistenceSettings ps;

	protected String dataStoreName="";
	
	private String projectName=""; 
	
	String supervisedOnlineFolder="" ;
	int nodeCount = 3;
	int srctype = 1;
	int surrogateActivated = 0;
	
	String startupTraceInfo ="" ; 
	
	
	// ---- some helpers ----
	transient DFutils fileutil = new DFutils();
	transient StringsUtil strgutil = new StringsUtil();
	transient PrintLog out = new PrintLog(2,false);
	transient ArrUtilities arrutil = new ArrUtilities();
	transient XmlStringHandling xMsg = new XmlStringHandling() ;

	private String configFile;

	
	// ========================================================================
	public DocoPropertiesHandler( DocumentObserverProperties properties){
		docoProperties = properties;
		
		
		out.setPrefix("[Doco-init]");
	}
	// ========================================================================
	
	@Override
	public void initializeDefaults() {
		
		String rootFolder="" ;
		
		
		         if ( docoProperties.getApplicationContext().contentEquals( DocumentObserverProperties._APP_CONTEXT_ITEXX)){
		        	 rootFolder = docoProperties.getSystemRootDir() ;		        	 
		        	 IniProperties.fluidSomProjectBasePath = rootFolder ;
		        	 IniProperties.saveIniProperties();
		         }else{
							// this comes from the out-most application layer (user, or remote control)  
							rootFolder = DocoServStartup.getProjectBasePath(); // IniProperties.fluidSomProjectBasePath ;
							rootFolder = DFutils.createPath(rootFolder, "/") ;
		         }	
		docoProperties.setPathToDocoSystemRootDir(rootFolder);	// within this dir all project base directories are located
										// sth like "D:/data/projects/"
		
		
		String prjname = DocoServStartup.getLastProjectName() ;
		
		ps.setPathToSomFluidSystemRootDir(rootFolder) ;
		ps.setProjectName( prjname );	// will be used also for output files
		            // sth like "bank2" , i.e. the simple name of a folder where all the project files are located 
		
		
		docoProperties.setDataStorageName( dataStoreName);       // if the persistence settings are available, the relative path will be used
		
							// this way we could also provide a file (or database) from an arbitrary location
							// docoProperties.addDataSource( srctype,"D:/data/projects/bank2/data/raw/bankn_d2.txt"); 
						
				
		//
		// [TGS] mode = single

		
		// variableSettings.setSomModelingMode(itemstr);
		
		
		docoProperties.setGlueInstanceType(1);
		docoProperties.setMessagingActive(false) ;					    // if true, the SomFluidFactory will start the glueClient ;
		   																// the SomFluid is then accessible through messaging (see the SomController application)
		docoProperties.setglueModuleMode( 0 ) ;							// 0 = off, others: act as source, receptor, or server

		// general environment

		docoProperties.activateMultithreadedProcesses(false);
		
		// assimilateVariableSettings();
		
		
		
		
		docoProperties.setShowDocoProgress( DocumentObserverProperties._DOCO_DISPLAY_PROGRESS_BASIC ) ;
		
		prepareStartupTraceInfo();
	}
	
	
	@Override
	public void initializeDefaultsFromFile(String inifilename) {
		
		
	}
	
		
	
	
	@Override
	public String getStartupTraceInfo() {
		 
		return startupTraceInfo;
	}

	
	/**
	 * 
	 * called after all settings have been defined
	 * creating a simple string that is used to create a "boot file": SomFluid then is able to know 
	 * about the project of the last run, in case a "resume" is requested
	 * 
	 * @return
	 */
	private void prepareStartupTraceInfo() {
		
		String cfgroot, userdir, lastproject, infoStr="";
		
		
		cfgroot =  docoProperties.getSystemRootDir() ;
		lastproject = docoProperties.getPersistenceSettings().getProjectName() ;
		
		// TODO better -> several categories: load file, modify it, write it back
		infoStr =  "cfgroot::"+cfgroot+"\n" +
		           "project::"+lastproject+"\n" ;
				                           	   

		
		startupTraceInfo = infoStr;
	}

	
	@Override
	public void setInstance(String description, int ...nodecount ) {
	 
		description = description.trim().toLowerCase();
		
		
		if (description.startsWith("doco")){
			docoProperties.setInstanceType( DocumentObserverFactory._INSTANCE_TYPE_DEFAULT) ;   
				// the main role the module is exhibiting, MANDATORY !!!
				// _SOM  _OPTIMIZER _TRANSFORM

			

		} // worker instance for default 
		
		 
		ps = docoProperties.getPersistenceSettings() ;
	}


	@Override
	public void setDataStoreName(String srcName) {
		
		dataStoreName = srcName;
	}

	public void setConfigFile(String filename) {
		
		configFile = filename;
	}
	

	
	
	
}
